package com.ts.api.module.app.upload.controller;

import com.gitee.apanlh.annotation.viewresolver.RequestParamVo;
import com.gitee.apanlh.util.base.StringUtils;
import com.gitee.apanlh.util.file.FileUtils;
import com.gitee.apanlh.util.io.FileIOUtils;
import com.gitee.apanlh.util.log.Log;
import com.gitee.apanlh.util.valid.Assert;
import com.gitee.apanlh.web.model.WebFile;
import com.gitee.apanlh.web.model.vo.RequestVO;
import com.gitee.apanlh.web.model.vo.ResultVO;
import com.ts.api.common.constant.BizEnum;
import com.ts.api.common.context.ChannelUserContext;
import com.ts.api.config.UploadPathConfig;
import com.ts.api.exp.BizException;
import com.ts.api.module.channel.entity.po.ChannelUserRegisterPO;
import com.ts.api.module.channel.service.DoubleWriteUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

/**
 *  AppUpload
 *  上传方法
 *  @author Pan
 */
@RestController
@RequestMapping("/app/upload")
public class AppUploadController {

    @Autowired
    private UploadPathConfig uploadPathConfig;
    @Autowired
    private DoubleWriteUserService doubleWriteUserService;

    /**
     *  人脸上传
     *
     *  @param  requestVO
     *  @return ResultVO
     */
    @RequestMapping("/face")
    public ResultVO<Object> uploadFace(@RequestParamVo RequestVO requestVO) {
        WebFile webFile = requestVO.getWebFile();
        Assert.isNotNullThrows(webFile, new BizException(BizEnum.EMPTY));

        //  人脸照
        MultipartFile file = webFile.getFile("file");
        if (file == null || file.getSize() == 0l) {
            throw new BizException(BizEnum.EMPTY);
        }

        String suffix = requestVO.get("suffix", String.class);
        Assert.isNotEmptyThrows(suffix, new BizException(BizEnum.EMPTY));

        String facePath = uploadPathConfig.getFacePath();
        try {
            String uniqueFileName = webFile.createUniqueFileName(file);
            if (uniqueFileName.lastIndexOf('.') == -1) {
                uniqueFileName = StringUtils.append(uniqueFileName, suffix);
            }
            FileUtils.create(facePath, uniqueFileName, false, true);
            FileIOUtils.writeToFile(file.getInputStream(), new File(facePath + uniqueFileName));

            ChannelUserRegisterPO userRegisterPO = new ChannelUserRegisterPO();
            userRegisterPO.setId(ChannelUserContext.getId());
            userRegisterPO.setFacePath(facePath + uniqueFileName);
            doubleWriteUserService.updateUser(userRegisterPO);
        } catch (Exception e) {
            Log.get().info("{}", e.getMessage(), e);
            return ResultVO.fail("上传失败");
        }
        return ResultVO.suc("上传成功");
    }

    /**
     *  身份证上传
     *
     *  @param  requestVO
     *  @return ResultVO
     */
    @RequestMapping("/idcard")
    public ResultVO<Object> uploadIdcard(@RequestParamVo RequestVO requestVO) {
        WebFile webFile = requestVO.getWebFile();
        Assert.isNotNullThrows(webFile, new BizException(BizEnum.EMPTY));

        //  身份证正反面
        MultipartFile fileFront = webFile.getFile("fileFront");
        MultipartFile fileBack = webFile.getFile("fileBack");

        if (fileFront == null || fileBack == null) {
            throw new BizException(BizEnum.EMPTY);
        }
        if (fileFront.getSize() == 0l || fileBack.getSize() == 0l) {
            throw new BizException(BizEnum.EMPTY);
        }

        String idcardFrontPath = uploadPathConfig.getIdcardFrontPath();
        String idcardBackPath = uploadPathConfig.getIdcardBackPath();

        String frontSuffix = requestVO.get("frontSuffix", String.class);
        String backSuffix = requestVO.get("backSuffix", String.class);
        Assert.isNotEmptyThrows(frontSuffix, new BizException(BizEnum.EMPTY));
        Assert.isNotEmptyThrows(backSuffix, new BizException(BizEnum.EMPTY));

        try {
            //  正面
            String uniqueFileName = webFile.createUniqueFileName(fileFront);
            if (uniqueFileName.lastIndexOf('.') == -1) {
                uniqueFileName = StringUtils.append(uniqueFileName, frontSuffix);
            }
            FileUtils.create(idcardFrontPath, uniqueFileName, false, true);
            FileIOUtils.writeToFile(fileFront.getInputStream(), new File(idcardFrontPath + uniqueFileName));


            //  反面
            String uniqueFileName2 = webFile.createUniqueFileName(fileBack);
            if (uniqueFileName2.lastIndexOf('.') == -1) {
                uniqueFileName2 = StringUtils.append(uniqueFileName2, backSuffix);
            }
            FileUtils.create(idcardBackPath, uniqueFileName2, false, true);
            FileIOUtils.writeToFile(fileBack.getInputStream(), new File(idcardBackPath + uniqueFileName2));

            ChannelUserRegisterPO userRegisterPO = new ChannelUserRegisterPO();
            userRegisterPO.setId(ChannelUserContext.getId());
            userRegisterPO.setIdcardFrontPath(idcardFrontPath + uniqueFileName);
            userRegisterPO.setIdcardBackPath(idcardBackPath + uniqueFileName2);
            doubleWriteUserService.updateUser(userRegisterPO);
        } catch (Exception e) {
            Log.get().info("{}", e.getMessage(), e);
            return ResultVO.fail("上传失败");
        }
        return ResultVO.suc("上传成功");
    }
}
